<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mongoose 使用 | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1. 命令行中创建并启动数据库 指定存储的位置和端口号  1mongod --dbpath --port=27018  code/mongo_demo  另开一个命令行窗口，进入 code/mongo_demo  1mongo 2. 使用 mongoose 链接数据库并创建 Collection 链接数据库 1234567891011121314const mongoose = require(&apos;">
<meta name="keywords" content="NodeJS">
<meta property="og:type" content="article">
<meta property="og:title" content="mongoose 使用">
<meta property="og:url" content="https://storm4542.github.io/archives/273360f2.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="1. 命令行中创建并启动数据库 指定存储的位置和端口号  1mongod --dbpath --port=27018  code/mongo_demo  另开一个命令行窗口，进入 code/mongo_demo  1mongo 2. 使用 mongoose 链接数据库并创建 Collection 链接数据库 1234567891011121314const mongoose = require(&apos;">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-30T08:24:26.258Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mongoose 使用">
<meta name="twitter:description" content="1. 命令行中创建并启动数据库 指定存储的位置和端口号  1mongod --dbpath --port=27018  code/mongo_demo  另开一个命令行窗口，进入 code/mongo_demo  1mongo 2. 使用 mongoose 链接数据库并创建 Collection 链接数据库 1234567891011121314const mongoose = require(&apos;">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-mongoose-使用" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      mongoose 使用
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2018-09-30T08:22:53.000Z" itemprop="datePublished">2018-09-30</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="1-命令行中创建并启动数据库"><a href="#1-命令行中创建并启动数据库" class="headerlink" title="1. 命令行中创建并启动数据库"></a>1. 命令行中创建并启动数据库</h3><ol>
<li>指定存储的位置和端口号</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongod --dbpath --port=27018  code/mongo_demo</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>另开一个命令行窗口，进入 code/mongo_demo</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongo</span><br></pre></td></tr></table></figure>
<h3 id="2-使用-mongoose-链接数据库并创建-Collection"><a href="#2-使用-mongoose-链接数据库并创建-Collection" class="headerlink" title="2. 使用 mongoose 链接数据库并创建 Collection"></a>2. 使用 mongoose 链接数据库并创建 Collection</h3><ol>
<li><p>链接数据库</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">'mongoose'</span>);</span><br><span class="line"><span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> uri = <span class="string">'mongodb://localhost/what_i_love'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> ObjectId = Schema.Types.ObjectId;</span><br><span class="line"></span><br><span class="line">mongoose.set(<span class="string">'useCreateIndex'</span>, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">mongoose.Promise = <span class="built_in">Promise</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> connection = mongoose.connect(uri, &#123;<span class="attr">useNewUrlParser</span>: <span class="literal">true</span>&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> db = mongoose.connection;</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建 Collection</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    name: &#123;<span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">unique</span>: <span class="literal">true</span>, <span class="attr">index</span>: <span class="number">1</span>&#125;,</span><br><span class="line">    age: &#123;<span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">max</span>: <span class="number">199</span>, <span class="attr">min</span>: [<span class="number">0</span>,<span class="string">'你还没生出来呢'</span>]&#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">const</span> UserModel = mongoose.model(<span class="string">'user'</span>, UserSchema);</span><br><span class="line"><span class="comment">//创建一个叫做 user 的 collection</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>操作 Collection</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">insert</span>(<span class="params">userInfo</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> UserModel.create(userInfo)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">getOneById</span>(<span class="params">id</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> UserModel.findOne(&#123;</span><br><span class="line">        _id: id</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">getOneByName</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> UserModel.findOne(&#123;</span><br><span class="line">        name</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">list</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> match = &#123;&#125;</span><br><span class="line">    <span class="keyword">const</span> flow = UserModel.find(match)</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">await</span> flow.exec()</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    insert,</span><br><span class="line">    getOneById,</span><br><span class="line">    getOneByName,</span><br><span class="line">    list,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="3-使用中"><a href="#3-使用中" class="headerlink" title="3. 使用中"></a>3. 使用中</h3><p> 一般来说链接数据库的方法单独放在一个地方。（第一步操作）</p>
<p>创建 Collection 并 操作Collection 放在一个地方。(第二步操作)</p>

      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="7b89c88d.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          QS中文文档
        
      </div>
    </a>
  
  
    <a href="1dd248d5.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">NodeJS 库 (持续更新)</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/273360f2.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
